package com.kdtech.analyse.outside;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * @name theguardian
 * @link http://www.theguardian.com/world/china
 * @author dhy
 */
public class TheguardianOutsideAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.theguardian.com/[a-z]*/[0-9]*/[a-z]*/[0-9]*/[a-z-?]*",
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		
		title =doc.select("h1.content__headline").text();

		try {
			Matcher matcher = Pattern.compile("(?i)[0-9]{4}/[a-z]{3}/[0-9]{2}").matcher(url);
			if(matcher.find())
			{
				Date parse = new SimpleDateFormat("yyyy/MMM/dd",Locale.US).parse(matcher.group());
				date = parse.getTime();
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		content=HtmlCleaner.getContentHtml(url,doc.select("div.content__article-body"));
		
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
